package com.wgts.wgts.mvp.view.activity.search;

import android.content.Context;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.control.recycleview.BaseLoadMoreAdapter;
import com.wgts.wgts.netmodle.resp.SearchShopResultEntity;
import com.wgts.wgts.tools.AppInfor;
import com.wgts.wgts.tools.CustomImageView;
import com.wgts.wgts.tools.MoHuGlide;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SearchResultGoodsAdapter extends BaseLoadMoreAdapter<SearchResultGoodsAdapter.SearchResultGoodsViewHolder> {

    private List<SearchShopResultEntity.ShopList> srcData;

    private Context context;

    private onItemClickListern onItemClickListern;


    public SearchResultGoodsAdapter(List<SearchShopResultEntity.ShopList> srcData, Context context) {
        super(context);
        this.srcData = srcData;
        this.context = context;
    }

    @Override
    public void onBindBodyViewHolder(@NonNull SearchResultGoodsViewHolder viewHolder, final int i) {
        CustomImageView.getInstance().customImage(context, viewHolder.imgProduct, (350/(double)750));
        if (i == 0) {
            viewHolder.placeholderView.setVisibility(View.GONE);
        }else {
            viewHolder.placeholderView.setVisibility(View.VISIBLE);
        }

        MoHuGlide.getInstance().glideImgsFormList(viewHolder.imgProduct, srcData.get(i).productSmallImage, context);

        viewHolder.tvProduce.setText(srcData.get(i).productName);

        viewHolder.tvPrice.setText("￥" + srcData.get(i).retailPrice);




        viewHolder.tvProduceSolve.setText("已售" + srcData.get(i).sellCount + "件");

        if(AppInfor.getInstance().getUserState() > 1){

            viewHolder.tvPrice.setTextColor(ContextCompat.getColor(context, R.color.black));

            viewHolder.placeholderText.setVisibility(View.VISIBLE);
            //viewHolder.tvOldPrice.getPaint().setFlags(null);
            viewHolder.tvOldPrice.setText(srcData.get(i).marketPrice);
            viewHolder.tvOldPrice.setTextSize(16);
            TextPaint tp = viewHolder.tvOldPrice.getPaint();
            tp.setFakeBoldText(true);
            viewHolder.tvOldPrice.setTextColor(ContextCompat.getColor(context, R.color.red_bg));
            viewHolder.tvOldPrice.setPaintFlags(viewHolder.tvOldPrice.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));

        }else{

            viewHolder.tvPrice.setTextColor(ContextCompat.getColor(context, R.color.red_bg));

            viewHolder.tvOldPrice.setText("￥" + srcData.get(i).marketPrice);
            viewHolder.placeholderText.setVisibility(View.GONE);
            viewHolder.tvOldPrice.setTextColor(ContextCompat.getColor(context, R.color.gray_text));
            viewHolder.tvOldPrice.setTextSize(11);
            TextPaint tp = viewHolder.tvOldPrice.getPaint();
            tp.setFakeBoldText(false);
            viewHolder.tvOldPrice.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
        }





        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListern != null) {
                    onItemClickListern.onItemClick(v, i);
                }
            }
        });
    }

    @NonNull
    @Override
    public SearchResultGoodsViewHolder onCreateBodyViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new SearchResultGoodsViewHolder(LayoutInflater.from(context).inflate(R.layout.home_page_time_action, viewGroup, false));
    }

    @Override
    public int getBodyItemCount() {
        return srcData.size();
    }

    @Override
    public int getBodyItemViewType(int position) {
        return 0;
    }

    public class SearchResultGoodsViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.placeholder_view)
        View placeholderView;
        @BindView(R.id.img_product)
        ImageView imgProduct;
        @BindView(R.id.tv_produce)
        TextView tvProduce;
        @BindView(R.id.tv_produce_solve)
        TextView tvProduceSolve;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.placeholder_text)
        TextView placeholderText;
        @BindView(R.id.tv_old_price)
        TextView tvOldPrice;
        @BindView(R.id.tv_state)
        TextView tvState;
        @BindView(R.id.linear_buy)
        LinearLayout linearBuy;
        @BindView(R.id.parent_ly)
        LinearLayout parentLy;
        @BindView(R.id.img_time_out)
        ImageView imgTimeOut;

        private View itemView;

        public SearchResultGoodsViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.itemView = itemView;
        }
    }

    public interface onItemClickListern {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListern(onItemClickListern onItemClickListern) {
        this.onItemClickListern = onItemClickListern;
    }
}
